package top.likeqc.leetcode.editor.cn;

/**
 * @see <a href="https://leetcode.cn/problems/check-if-matrix-is-x-matrix">2319. 判断矩阵是否是一个 X 矩阵</a>
 */
public class CheckIfMatrixIsXMatrix {
    public static void main(String[] args) {
        Solution solution = new CheckIfMatrixIsXMatrix().new Solution();
    }
    // leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public boolean checkXMatrix(int[][] grid) {
            for (int i = 0; i < grid.length; i++) {
                for (int j = 0; j < grid[i].length; j++) {
                    if (isDiagonal(i, j, grid.length) && grid[i][j] == 0) {
                        return false;
                    }
                    if (!isDiagonal(i, j, grid.length) && grid[i][j] != 0) {
                        return false;
                    }
                }
            }
            return true;
        }

        /**
         * 判断 (x, y) 坐标是否位于正方形的对角线
         *
         * @param x
         * @param y
         * @return true, if (x, y) are located on the diagonal of the square ,else false
         */
        public boolean isDiagonal(int x, int y, int n) {
            return x == y || (x == n - y - 1);
        }
    }
    // leetcode submit region end(Prohibit modification and deletion)

}
